package com.qxsx.midi.activity.exam;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.qxsx.midi.R;
import com.qxsx.midi.activity.base.BaseActivity;
import com.qxsx.midi.adapter.CateAdapter2;
import com.qxsx.midi.config.DataManager;
import com.qxsx.midi.utils.CustomProgress;
import com.qxsx.midi.utils.MyNet;
import com.qxsx.midi.utils.callBackJsonArray;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Joney on 2015/9/7.
 * 备考指南
 */
public class ExamHandBookActivity extends BaseActivity implements OnClickListener {
    private CustomProgress customProgress;
    //备考指南的web
    private WebView wv_handbook = null;
    private WebSettings mWebSettings;
    //等级，专业
    private TextView tv_major, tv_grade;
    private LinearLayout ll_major, ll_grade;
    private LinearLayout ll_menu;
    private CheckBox ck_major, ck_grade;
    //分类
    private static String grade = "1"; //初始化等级
    private static String major = "2172405d-17bb-5f40-da92-cf9cc285a980"; //初始化专业
    private ListView lv_cate;
    private CateAdapter2 cateAdapter;
    private View mPopupWindowView;
    private static PopupWindow mPopupWindow;
    private static int LEFT_P;  //左边POP选中位置
    private static int RIGHT_P; //右边POP选中位置

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exam_handbook);
        setTitle("备考指南");
        setNavBtn(R.mipmap.back, 0);
        findById();
        getDataList();

    }


    /**
     * 备考指南接口
     */
    private void getDataList() {
        customProgress = CustomProgress.show(ExamHandBookActivity.this, "正在加载中，请稍后....", true, null);
        JSONArray array = new JSONArray();
        JSONObject object = new JSONObject();

        try {
            object.put("major_id", major);
            object.put("grade_num", grade);
            array.put(object);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        MyNet.doPostJsonArray("509", array, new callBackJsonArray() {
            @Override
            public void run(Object code, Object message, JSONArray array) {
                Log.w("code", code + "");
                Log.w("message", message + "");
                Log.w("array", array + "");
                try {
                    JSONObject object1 = array.getJSONObject(0);
                    String url = object1.getString("url");
                    inItWebView(url);
                    customProgress.dismiss();
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });


    }

    private void inItWebView(String url) {
        wv_handbook.requestFocus();
        wv_handbook.getSettings().setSupportZoom(true);
        wv_handbook.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
        wv_handbook.getSettings().setBuiltInZoomControls(true);
        wv_handbook.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        mWebSettings = wv_handbook.getSettings();
        mWebSettings.setJavaScriptEnabled(true);
        mWebSettings.setUseWideViewPort(true);
        mWebSettings.setPluginState(PluginState.ON);
        wv_handbook.setWebViewClient(new Callback());
        mWebSettings.setLoadWithOverviewMode(true);
        wv_handbook.setHapticFeedbackEnabled(false);

        wv_handbook.loadUrl(url);

    }


    private class Callback extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return (false);
        }
    }

    private void findById() {
        wv_handbook = (WebView) findViewById(R.id.wv_handbook);
        tv_major = (TextView) findViewById(R.id.tv_major);
        tv_major.setOnClickListener(this);

        tv_grade = (TextView) findViewById(R.id.tv_grade);
        tv_grade.setOnClickListener(this);

        ll_major = (LinearLayout) findViewById(R.id.ll_major);
        ll_major.setOnClickListener(this);

        ll_grade = (LinearLayout) findViewById(R.id.ll_grade);
        ll_grade.setOnClickListener(this);

        ll_menu = (LinearLayout) findViewById(R.id.ll_menu);
        ll_menu.setOnClickListener(this);

        ck_grade = (CheckBox) findViewById(R.id.ck_grade);
        ck_major = (CheckBox) findViewById(R.id.ck_major);

        tv_major.setSelected(true);
        ll_major.setClickable(true);

    }

    /**
     * @param component
     * @param v
     */
    @Override
    protected void HandleTitleBarEvent(TitleBar component, View v) {
        switch (component) {
            case LEFT:
                finish();
                break;
        }
    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_major:

                // 获取自定义布局文件视图
                mPopupWindowView = LayoutInflater.from(ExamHandBookActivity.this).inflate(R.layout.major_pop_item, null, false);
                lv_cate = (ListView) mPopupWindowView.findViewById(R.id.lv_cate);
                // 创建PopupWindow实例,LayoutParams.MATCH_PARENT分别是宽度和高度
                mPopupWindow = new PopupWindow(mPopupWindowView, ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT, true);
                ColorDrawable dw = new ColorDrawable(ExamHandBookActivity.this.getApplicationContext().getResources().getColor(R.color.theme_transparency));
                mPopupWindow.setBackgroundDrawable(dw);
                // 这里是位置显示方式,在屏幕的右侧,v(标题栏)之下
                mPopupWindowView.setFocusable(true);
                mPopupWindowView.setFocusableInTouchMode(true);
                mPopupWindow.setBackgroundDrawable(ExamHandBookActivity.this.getResources().getDrawable(R.mipmap.study_call_menu_bg));
                mPopupWindowView.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        if (mPopupWindow.isShowing()) {
                            mPopupWindow.dismiss();
                        }
                    }
                });
                mPopupWindowView.setOnKeyListener(new View.OnKeyListener() {
                    @Override
                    public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
                        if (arg1 == KeyEvent.KEYCODE_BACK) {
                            if (mPopupWindow != null) {
                                mPopupWindow.dismiss();
                            }
                        }
                        return false;
                    }
                });
                cateAdapter = new CateAdapter2(ExamHandBookActivity.this, DataManager.getManage().getMajorList2(), "LEFT");
                lv_cate.setAdapter(cateAdapter);
                cateAdapter.setChoose(LEFT_P);
                lv_cate.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        cateAdapter.setChoose(position);
                        LEFT_P = position;
                        tv_major.setText( DataManager.getManage().getMajorList2().get(position).getMajor_name());
                        if (mPopupWindow != null) {
                            cateAdapter.setChoose(position);
                            major = DataManager.getManage().getMajorList2().get(position).getMajor_id();
                            getDataList();
                            mPopupWindow.dismiss();
                        }
                    }
                });
                mPopupWindow.showAsDropDown(v, 0, 5);
                mPopupWindow.setFocusable(true);
                mPopupWindow.setOutsideTouchable(true);
                tv_major.setSelected(true);
                tv_grade.setSelected(false);
                ll_major.setClickable(true);
                ll_grade.setClickable(false);
                break;
            case R.id.tv_grade:

                // 获取自定义布局文件视图
                mPopupWindowView = LayoutInflater.from(ExamHandBookActivity.this).inflate(R.layout.major_pop_item, null, false);
                lv_cate = (ListView) mPopupWindowView.findViewById(R.id.lv_cate);
                // 创建PopupWindow实例,LayoutParams.MATCH_PARENT分别是宽度和高度
                mPopupWindow = new PopupWindow(mPopupWindowView, ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT, true);
                ColorDrawable dw2 = new ColorDrawable(ExamHandBookActivity.this.getApplicationContext().getResources().getColor(R.color.theme_transparency));
                mPopupWindow.setBackgroundDrawable(dw2);
                // 这里是位置显示方式,在屏幕的右侧,v(标题栏)之下
                mPopupWindowView.setFocusable(true);
                mPopupWindowView.setFocusableInTouchMode(true);
                mPopupWindow.setBackgroundDrawable(ExamHandBookActivity.this.getResources().getDrawable(R.mipmap.study_call_menu_bg));
                mPopupWindowView.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        if (mPopupWindow.isShowing()) {
                            mPopupWindow.dismiss();
                        }
                    }
                });
                mPopupWindowView.setOnKeyListener(new View.OnKeyListener() {
                    @Override
                    public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
                        if (arg1 == KeyEvent.KEYCODE_BACK) {
                            if (mPopupWindow != null) {
                                mPopupWindow.dismiss();
                            }
                        }
                        return false;
                    }
                });
                mPopupWindow.showAsDropDown(v, 0, 5);
                mPopupWindow.setFocusable(true);
                mPopupWindow.setOutsideTouchable(true);
                cateAdapter = new CateAdapter2(ExamHandBookActivity.this, DataManager.getManage().getGradeList2(), "RIGHT");
                lv_cate.setAdapter(cateAdapter);
                cateAdapter.setChoose(RIGHT_P);
                lv_cate.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        cateAdapter.setChoose(position);
                        RIGHT_P = position;
                        tv_grade.setText(DataManager.getManage().getGradeList2().get(position).getMajor_name());
                        if (mPopupWindow != null) {
                            cateAdapter.setChoose(position);
                            grade = DataManager.getManage().getGradeList2().get(position).getMajor_id();
                            getDataList();
                            mPopupWindow.dismiss();
                        }
                    }
                });
                tv_major.setSelected(false);
                tv_grade.setSelected(true);
                ll_major.setClickable(false);
                ll_grade.setClickable(true);
                break;
            case R.id.ll_major:
                break;
            case R.id.ll_grade:
                break;

        }
    }
}
